package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.TrainBean;
import cst.neu.yuejian_31.iface.TrainDetailIface;
import cst.neu.yuejian_31.iface.TrainDetailListener;
import cst.neu.yuejian_31.iface.TrainIface;
import cst.neu.yuejian_31.iface.TrainListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.TrainDetailService;
import cst.neu.yuejian_31.service.TrainService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/10/26.
 */

public class TrainModel implements TrainIface,TrainDetailIface {
        private Retrofit retrofit;
        //    private String BASEURL="http://10.0.2.2/neu16_yuejian/api/Club/";
        public TrainModel(){
            retrofit=new Retrofit.Builder()
                    .baseUrl(Constant.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        @Override
        public void getTrainList(final TrainListener TrainListener) {
            TrainService TrainService = retrofit.create(TrainService.class);
            Call<List<TrainBean>> call
                    = TrainService.getTrainlist();
            call.enqueue(new Callback<List<TrainBean>>() {
                @Override
                public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        TrainListener.onResponse(response.body());
                    } else {
                        TrainListener.onFail("FAIL");

                    }
                }

                @Override
                public void onFailure(Call<List<TrainBean>> call, Throwable t) {

                    TrainListener.onFail(t.toString());
                }
            });
        }
    @Override
    public void gettraindetail(String train_id,final TrainDetailListener trainDetailListener) {
       TrainDetailService trainDetailService=retrofit.create(TrainDetailService.class);
        Call<TrainBean>call
                = trainDetailService.gettraindetail(train_id);
        call.enqueue(new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    trainDetailListener.onResponse(response.body());
                }
                else {
                   trainDetailListener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {

               trainDetailListener.onFail(t.toString());
            }
        });
    }
}